﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Diagnostics;
using CPAScoringPerformanceTester.Models;

namespace CPAScoringPerformanceTester.Windows
{
    /// <summary>
    /// RobotWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RobotWindow : Window, INotifyPropertyChanged
    {
        private const string USERNAME_PATTERN = "user{1-10}";
        private const string PASSWORD_PATTERN = "123456";
        public event RobotAddedEventHandler RobotAdded;
        public event PropertyChangedEventHandler PropertyChanged;

        private bool singleMode;
        public bool SingleMode
        {
            get { return singleMode; }
            set
            {
                singleMode = value;
                OnPropertyChanged("SingleMode");
            }
        }

        private bool multipleMode;
        public bool MultipleMode
        {
            get { return multipleMode; }
            set
            {
                multipleMode = value;
                OnPropertyChanged("MultipleMode");
            }
        }

        public Robot NewRobot { get; set; }

        private string usernamePattern;
        public string UsernamePattern
        {
            get { return usernamePattern; }
            set
            {
                usernamePattern = value;
                OnPropertyChanged("UsernamePattern");
            }
        }

        private string passwordPattern;
        public string PasswordPattern
        {
            get { return passwordPattern; }
            set
            {
                passwordPattern = value;
                OnPropertyChanged("PasswordPattern");
            }
        }

        public RelayCommand AddCommand { get; set; }
        public RelayCommand CloseCommand { get; set; }

        public RobotWindow()
        {
            this.NewRobot = new Robot();
            AddCommand = new RelayCommand(Add);
            CloseCommand = new RelayCommand(() => this.Close());
            InitializeComponent();
            MultipleMode = true;
            UsernamePattern = USERNAME_PATTERN;
            PasswordPattern = PASSWORD_PATTERN;
        }

        private void Add()
        {
            if (SingleMode)
            {
                OnRobotAdded(this.NewRobot);
            }
            else
            {
                try
                {
                    List<string> users = GetPatternValue(UsernamePattern);
                    List<string> passwords = GetPatternValue(PasswordPattern);

                    List<Robot> robots = new List<Robot>();
                    for (int i = 0; i < users.Count; i++)
                    {
                        robots.Add(new Robot()
                        {
                            Username = users[i],
                            Password = passwords[Math.Min(i, passwords.Count -1)]
                        });
                    }

                    OnRobotAdded(robots);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    MessageBox.Show("Failed! Wrong Pattern...");
                    return;
                }
            }

            this.Close();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void OnRobotAdded(Robot robot)
        {
            if (RobotAdded != null)
            {
                RobotAdded(this, new RobotAddedEventArgs(robot));
            }
        }

        private void OnRobotAdded(List<Robot> robots)
        {
            if (RobotAdded != null)
            {
                RobotAdded(this, new RobotAddedEventArgs(robots));
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private List<string> GetPatternValue(string input)
        {
            Match match = Regex.Match(input, @"\{[0-9]+-[0-9]+\}");
            if (match == null || match.Length <= 0) return new List<string>() { input };

            int[] boundary = match.Value.Substring(1, match.Value.Length - 2).Split('-').Select(m => Convert.ToInt32(m)).ToArray();
            int min = boundary[0];
            int max = boundary[1];

            List<string> values = new List<string>();
            for (int i = min; i <= max; i++)
            {
                values.Add(input.Replace(match.Value, i.ToString()));
            }

            return values;
        }
    }
}
